package gui.sejour;

import gui.FenetrePrincipale;
import gui.PanelChambre;
import gui.PanelClient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.LigneReservation;
import model.Reservation;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Controleur.ControleurSejour;

import java.awt.Rectangle;
import java.util.ArrayList;

public class FenetreSelectionLigneRes extends FenetrePrincipale {
	private JButton btnSelectionner;
	private JList lstReservation;
	private PanelClient panelClient;
	
	Reservation reservation = null;
	private JLabel lblHeaderLigneReservation;

	public FenetreSelectionLigneRes(ActionListener ctrl, Reservation res){
		setBounds(new Rectangle(0, 0, 510, 497));
		reservation = res;
		afficher(ctrl);
	}
	
	@Override
	public void afficher(final ActionListener ctrl) {
		getContentPane().setLayout(null);
		init();
		
		panelClient = new PanelClient();
		panelClient.fillWithClient(reservation.getClient());
		getContentPane().add(panelClient);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 230, 467, 169);
		getContentPane().add(scrollPane);
		
		lstReservation = new JList();
		majLigneReservation(reservation);
		lstReservation.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (lstReservation.getSelectedIndex() >= 0) {
					ctrl.actionPerformed(new ActionEvent(this, lstReservation.getSelectedIndex(), String.valueOf(ControleurSejour.LIGNE_RES_SELECT_LIST)));
					btnSelectionner.setEnabled(true);
				}
			}
		});
		scrollPane.setViewportView(lstReservation);
		
		btnSelectionner = new JButton("S\u00E9lectionner");
		btnSelectionner.setEnabled(false);
		btnSelectionner.setBounds(12, 412, 126, 25);
		btnSelectionner.addActionListener (ctrl);
		btnSelectionner.setActionCommand(String.valueOf(ControleurSejour.SELECTIONNER_LIGNE_RES));
		getContentPane().add(btnSelectionner);
		
		lblHeaderLigneReservation = new JLabel(PanelChambre.HEADER_LISTE_RESERVATION);
		lblHeaderLigneReservation.setBounds(12, 201, 467, 16);
		getContentPane().add(lblHeaderLigneReservation);

		setVisible(true);
	}
	
	public void majLigneReservation(Reservation resActuel){
		ArrayList<String> listeReservation = new ArrayList<String>();
		
		for (LigneReservation itLigne : resActuel){
			if (!itLigne.isSejourCree()){
				listeReservation.add(itLigne.toStringFormat());
			}
		}
		
		this.lstReservation.setListData(listeReservation.toArray());
	}
}
